<?php

namespace app\admin\controller\expenses;

use app\common\controller\Backend;

/**
 * Created by PhpStorm.
 * FileName: Index.php
 * User: Administrator
 * Date: 2017/11/01
 * Time: 13:42
 */

class Index extends Backend
{

    protected $model = null;
    protected $communityModel = null;
    protected $projectModel = null;
    protected $houseModel = null;
    protected $payModel = null;
    //检索时匹配的字段
    protected $searchfields = null;
    protected $noNeedRight = ['selectpage', 'get_project_by_cm_code'];
    protected $companyModel = null;
    protected $documentModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Expenses');
        $this->communityModel = model('Community');
        $this->projectModel = model('ExpensesProject');
        $this->houseModel = model('House');
        $this->payModel = model('ExpensesPay');
        $this->companyModel = model('Company');
        $this->documentModel = model('Document');
        $this->view->assign('community', $this->communityModel->where(array('code' => array('in', parent::getCommunityIdByAuth())))->field('code,name')->select());
        $this->view->assign('pay', $this->payModel->field('code,name')->select());
        $companywhere = array();
        $companyid = 0;
       if ($_GET['cid']) {
            $companyid = $_GET['cid'];          
        }

        $this->view->assign('company', $this->companyModel->where('companytype=1')->field('id,companyname')->select());
        // $docwhere = array();
        // if ($_GET["docid"]) {
        //     $docwhere = array(array('id', '=', $_GET["docid"]));
        // }
        $this->view->assign("docid", $_GET["docid"]);
        $this->view->assign("cid", $companyid);
        $this->view->assign('document', $this->documentModel->field('id,name')->select());
        }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            return $this->handleSearch($this->searchfields);
        }
        return $this->view->fetch();
    }

    public function detail($ids = null)
    {
        return parent::modify($ids);
    }

    public function docitems(){
        $docid=isset($_GET['docid']) ? $_GET['docid'] : 0;
        $doc=$this->documentModel->where("id",$docid)->find();      
        $this->view->assign('doc', $doc);
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $this->request->post(array('row' => $params));
        }
      
        return parent::create();
    }

    //修复错误
    public function itemsfix(){
        $docid=isset($_GET['docid']) ? $_GET['docid'] : 0;
        $doc=$this->documentModel->where("id",$docid)->find();      
        
        
    }


    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
           
            $map['docid']=$params['docid'];
            $map['house_code']=$params['house_code'];
            $map['payment_date']=$params['payment_date'];
           $o=$this->model->where($map)->find();
            if(!empty($o))
            {
                $this->error("相同记录已存在");
                return;
            }
            $this->request->post(array('row' => $params));
        }

        return parent::create();
     }

    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $this->request->post(['row' => $params]);
        }
        return parent::modify($ids, 'edit');
    }

    public function del($ids = null)
    {
        $where = array(
            'id' => array('in', $ids)
        );
        parent::delete($where);
    }

    public function clear()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
        $where = array(
            'docid' => array('in', $params['docid']),
            'house_code' => array('=',$params['house_code'])
        );
        parent::delete($where);
        }
    }

    public function selectpage()
    {
        return parent::selectpage();
    }

    private function handleSearch($searchfields = null)
    {
        $append = array(
            array('community_code', 'in', parent::getCommunityIdByAuth())
        );
        $append = array_merge($append, $this->buildCommonSearch());
        if ($_GET["docid"]) {
            $append = array_merge($append,array(array('docid', '=', $_GET["docid"])));
        }
        
        list($where, $sort, $order, $offset, $limit, $orderParams) = $this->buildparams($searchfields, null, $append);
        $total = $this->model->where($where)->count();
        $list = $this->model->with('community,project,house,pay,document')->order('payment_date', 'asc')->where($where)->order($orderParams)->limit($offset, $limit)->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }

    public function get_project_by_cm_code()
    {
        $result = array();
        $cmCode = $this->request->request('community_code');
        $project = $this->projectModel->getProjectByCMCode($cmCode);
        $house = $this->houseModel->getHouseByCMCode($cmCode);
        $result['project'] = $project;
        $result['house'] = $house;
        return json($result);
    }

    /**
     * 自定义搜索
     * @return array
     */
    private function buildCommonSearch()
    {
        $where = array();
        $searchs = $this->request->request('query/a');
        if ($searchs['community_code']) {
            $where[] = array('community_code', '=', $searchs['community_code']);
        }
        if ($searchs['project_code']) {
            $where[] = array('project_code', '=', $searchs['project_code']);
        }
        if ($searchs['house_code']) {
            $where[] = array('house_code', '=', $searchs['house_code']);
        }
        if ($searchs['docid']) {
            $where[] = array('docid', '=', $searchs['docid']);
        }
        if ($searchs['create_time_begin']) {
            $where[] = array('create_time', '>=', strtotime($searchs['create_time_begin'] . '00:00:00'));
        }
        if ($searchs['create_time_end']) {
            $where[] = array('create_time', '<=', strtotime($searchs['create_time_end'] . '23:59:59'));
        }
        return $where;
    }
}
